﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.COM;
using MINERP.BLL.Statistics;
using MINERP.MODEL.FoundationModel;
using DevExpress.XtraEditors.Controls;
using MINERP.ComControls.Extension;

namespace MINERP.Statistics.Forms.ProfessionalReports
{
    public partial class FrmDeliveryStat : Form
    {
        public FrmDeliveryStat()
        {
            InitializeComponent();
        }

        private static FrmDeliveryStat _instance;
        public static FrmDeliveryStat GetInstance
        {
            get
            {
                if (_instance == null || _instance.IsDisposed)
                {
                    _instance = new FrmDeliveryStat();
                }
                return _instance;
            }
        }

        private NewPerformanceBLL _bll;
        private PerformanceBLL _oldBll;
        private void FrmDeliveryStat_Load(object sender, EventArgs e)
        {
            _bll = new NewPerformanceBLL();
            _oldBll = new PerformanceBLL();
            BandingGroup(_oldBll.GetAllGroup());
        }
        private void BindWorkerList(int GroupId)
        {
            List<UserInfo> userList = new List<UserInfo>();
            if (GroupId >=0)
            {
                userList = _bll.GetUserListByGroupId(GroupId);
            }
            cmkUser.Properties.DataSource = userList;
            cmkUser.Properties.DisplayMember = "UserName";
            cmkUser.Properties.ValueMember = "UserID";
        }
        void BandingGroup(List<Tb_WorkerGroup_Info> list)
        {
            cmbGroup.DisplayMember = "GroupName";
            cmbGroup.ValueMember = "GroupId";
            cmbGroup.DataSource = list;
        }

        private void cmbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            int GroupId = ((Tb_WorkerGroup_Info)this.cmbGroup.SelectedItem).GroupId;
            BindWorkerList(GroupId);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (dtStart.DateTime > dtEnd.DateTime)
                {
                    MessageBox.Show("起始日期必须小于结束日期");
                    dtStart.DateTime = dtEnd.DateTime.Date;
                    this.Cursor = Cursors.Default;
                    return;
                }

                List<int> selectedUserIDList = new List<int>();
                foreach (CheckedListBoxItem item in cmkUser.Properties.Items)
                {
                    if (item.CheckState == CheckState.Checked)
                    {
                        selectedUserIDList.Add((int)item.Value);
                    }
                }
                if (selectedUserIDList.Count == 0)
                {
                    MessageBox.Show("请选择员工");
                }
                gcDeliverPerformance.DataSource = _bll.GetDeliverStat(selectedUserIDList, dtStart.DateTime, dtEnd.DateTime);
                gcDeliverPerformance.RefreshDataSource();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //DevGridViewExport.ExportToExcel("拼版员业绩", gridView2);
            DevControlExtension.ExportToExcel(gcDeliverPerformance, this, "发货员业绩");
            //SaveFileDialog saveFileDialog = new SaveFileDialog();
            //saveFileDialog.Filter = "文本文件 (*.txt)|*.txt|pdf(*.pdf)|*.pdf|电子表格(*.xls)|*.xls|html(*.html)|*.html";
            ////saveFileDialog.AddExtension = true;
            //saveFileDialog.FilterIndex = 3;
            //saveFileDialog.RestoreDirectory = true;
            //if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
            //    string fileName = saveFileDialog.FileName;
            //    if (saveFileDialog.OpenFile() != null)
            //    {
            //        switch (saveFileDialog.FilterIndex)
            //        {
            //            case 3:
            //                gcDeliverPerformance.ExportToXls(fileName);
            //                return;
            //            case 4:
            //                gcDeliverPerformance.ExportToHtml(fileName);
            //                return;
            //            case 2:
            //                gcDeliverPerformance.ExportToPdf(fileName);
            //                return;
            //            case 1:
            //                this.gcDeliverPerformance.ExportToText(fileName);
            //                return;
            //            default:
            //                break;
            //        }
            //    }

            //}

        }
    }
}
